package cn.sdnu.database.service;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.ReentrantLock;

import cn.sdnu.database.utils.TableGenerator;
import cn.sdnu.database.utils.Utils;

public class Describe {
	private static Describe describe = null;
	private static ReentrantLock lock = new ReentrantLock();

	private Describe() {

	}

	/**
	 * 
	 * @return 一个describe 实例
	 */
	public static Describe getInstance() {
		if (describe == null) {
			lock.lock();
			try {
				if (describe == null) {
					describe = new Describe();
				}
			} finally {
				lock.unlock();
			}
		}
		return describe;
	}

	/**
	 * 处理 SQL语句
	 * 
	 * @param sql 要被处理的SQL语句
	 */
	public void sqlHandle(String sql) {
		if (sql.startsWith("describe ")) {
			if (Use.getInstance().getDatabaseUsed() != null) {
				String tableName = sql.substring(9, sql.length() - 2);
				if (Utils.getTables(Use.getInstance().getDatabaseUsed()).contains(tableName)) {
					List<String> headersList = new ArrayList<String>();
					headersList.add("Field");
					headersList.add("Type");
					headersList.add("Extra");

					List<List<String>> rowsList = new ArrayList<List<String>>();
					List<String> columns = Utils.getColumns(tableName, Use.getInstance().getDatabaseUsed());
					List<String> types = Utils.getTypes(tableName, Use.getInstance().getDatabaseUsed());

					List<String> extra = Utils.getExtra(tableName, Use.getInstance().getDatabaseUsed());

					for (int i = 0; i < columns.size(); i++) {
						List<String> rowList = new ArrayList<String>();
						rowList.add(columns.get(i));
						rowList.add(types.get(i));
						rowList.add(extra.get(i));
						rowsList.add(rowList);
					}
					System.out.println(TableGenerator.generateTable(headersList, rowsList));
					System.out.println(rowsList.size() + " rows in set\n");
				} else {
					System.out.println("ERROR: Table '" + Use.getInstance().getDatabaseUsed() + "." + tableName
							+ "' doesn't exist");
				}
			} else {
				System.out.println("ERROR: No database selected");
			}
		} else if (sql.startsWith("desc ")) {
			if (Use.getInstance().getDatabaseUsed() != null) {
				String tableName = sql.substring(5, sql.length() - 2);
				if (Utils.getTables(Use.getInstance().getDatabaseUsed()).contains(tableName)) {
					List<String> headersList = new ArrayList<String>();
					headersList.add("Field");
					headersList.add("Type");
					headersList.add("Extra");

					List<List<String>> rowsList = new ArrayList<List<String>>();
					List<String> columns = Utils.getColumns(tableName, Use.getInstance().getDatabaseUsed());
					List<String> types = Utils.getTypes(tableName, Use.getInstance().getDatabaseUsed());

					List<String> extra = Utils.getExtra(tableName, Use.getInstance().getDatabaseUsed());
					for (int i = 0; i < columns.size(); i++) {
						List<String> rowList = new ArrayList<String>();
						rowList.add(columns.get(i));
						rowList.add(types.get(i));
						rowList.add(extra.get(i));
						rowsList.add(rowList);
					}

					System.out.println(TableGenerator.generateTable(headersList, rowsList));
					System.out.println(rowsList.size() + " rows in set\n");
				} else {
					System.out.println("ERROR: Table '" + Use.getInstance().getDatabaseUsed() + "." + tableName
							+ "' doesn't exist");
				}
			} else {
				System.out.println("ERROR: No database selected");
			}
		} else {
			System.out.println("You have some syntax error, please try again");
		}
	}

}
